package com.taoism.utils;

import javax.persistence.Id;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

/**
 * 对象工具
 *
 * @author 李嘉
 * @version 1.0
 * @Description 对象工具
 * @date 2019-07-14 14:06
 */
public class ObjectUtil {

    private final static String IGNORE_FIELD = "serialVersionUID";

    /**
     * 赋值相同对象的值
     *
     * @param setObject 需要设置值的对象
     * @param getObject 需要获取值的对象
     */
    public static void assignment(Object setObject, Object getObject) {
        Field[] setFields = setObject.getClass().getDeclaredFields();
        Field[] getFields = getObject.getClass().getDeclaredFields();
        try {
            for (Integer i = 0; i < setFields.length; i++) {
                if (setFields[i].getName().equalsIgnoreCase(getFields[i].getName()) && !setFields[i].getName().equalsIgnoreCase(IGNORE_FIELD)) {
                    Field getField = getFields[i];
                    Id idAnnotation = getField.getDeclaredAnnotation(Id.class);
                    if (idAnnotation == null) {
                        getField.setAccessible(true);
                        Object getValue = getField.get(getObject);
                        if (!StringUtil.isEmpty(getValue)) {
                            Field setField = setFields[i];
                            setField.setAccessible(true);
                            setField.set(setObject, getValue);
                            setField.setAccessible(false);
                        }
                        getField.setAccessible(false);
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 赋值相同对象的值
     *
     * @param setObject 需要设置值的对象
     * @param getObject 需要获取值的对象
     */
    public static void assignmentHana(Object setObject, Object getObject) {
        Field[] setFields = setObject.getClass().getDeclaredFields();
        Field[] getFields = getObject.getClass().getDeclaredFields();
        List<Field> fieldList = Arrays.asList(getFields);
        try {
            for (Integer i = 0; i < setFields.length; i++) {
                if (!setFields[i].getName().equalsIgnoreCase(IGNORE_FIELD)) {
                    Integer finalI = i;
                    Field getField = fieldList.stream().filter(t -> t.getName().equalsIgnoreCase(setFields[finalI].getName())).findFirst().get();
                    Id idAnnotation = getField.getDeclaredAnnotation(Id.class);
                    if (idAnnotation == null) {
                        getField.setAccessible(true);
                        Object getValue = getField.get(getObject);
                        if (!StringUtil.isEmpty(getValue)) {
                            Field setField = setFields[i];
                            setField.setAccessible(true);
                            setField.set(setObject, getValue);
                            setField.setAccessible(false);
                        }
                        getField.setAccessible(false);
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
